package com.xkdx.guangguang.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils.TruncateAt;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.shareclass.ShopCommentary;
import com.xkdx.guangguang.wiget.TextViewEllipsize;

// 商场评论适配
public class ShopMoreCommentAdapter extends BaseAdapter {
	String url;
	Context context;
	List<ShopCommentary> commlist;
	ImageLoader imageLoader;
	String shopID, shopName;
	boolean moreSize;
	DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.shop_empty)
			.showImageForEmptyUri(R.drawable.shop_empty).showImageOnFail(R.drawable.shop_empty)
			.resetViewBeforeLoading().cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
			.bitmapConfig(Bitmap.Config.RGB_565).build();

	public ShopMoreCommentAdapter(Context context, List<ShopCommentary> list, ImageLoader imageloder, String shopID,
			String shopName) {
		super();
		this.imageLoader = imageloder;
		this.commlist = list;
		this.context = context;
		this.shopID = shopID;
		this.shopName = shopName;
	}

	@Override
	public boolean isEnabled(int position) {
		return super.isEnabled(position);
	}

	@Override
	public int getCount() {

		return commlist.size();
	}

	@Override
	public Object getItem(int position) {

		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		view = LayoutInflater.from(context).inflate(R.layout.shop_commentaryitem, null);
		ImageView userhead = (ImageView) view.findViewById(R.id.iv_shopcommitem_userhead);
		TextView username = (TextView) view.findViewById(R.id.tv_shopcommitem_username);
		TextView commentaryDate = (TextView) view.findViewById(R.id.tv_shopcommitem_date);
		final TextViewEllipsize commentaryinfos = (TextViewEllipsize) view.findViewById(R.id.tv_shopcomitem_infos);
		final ImageView morecommentary = (ImageView) view.findViewById(R.id.btn_spcommitem_loadmore);

		username.setText(commlist.get(position).getNickName() + ":");

		if (commlist.get(position).getCreateTime() != null && !(commlist.get(position).getCreateTime().equals(""))) {
			commentaryDate.setText(DateParse(commlist.get(position).getCreateTime()));
		} else {
			commentaryDate.setText("");
		}

		commentaryinfos.setText(commlist.get(position).getCommentContent());
		commentaryinfos.setEllipsize(TruncateAt.END);
		commentaryinfos.setMaxLines(3);
		String picUrl = commlist.get(position).getUserAvatars();
		// Log.i("picUrl",picUrl);
		imageLoader.displayImage(picUrl, userhead, options);

		morecommentary.setOnClickListener(new OnClickListener() {
			boolean flag = true;

			@Override
			public void onClick(View v) {

				if (flag) {
					int num = commentaryinfos.getLineCount();
					if (num >= 3) {
						commentaryinfos.setEllipsize(null);
						commentaryinfos.setMaxLines(100);
						morecommentary.setImageResource(R.drawable.commentaryclose);
						flag = false;
					}

					// }
				} else {
					int num = commentaryinfos.getLineCount();
					if (num >= 3) {
						morecommentary.setImageResource(R.drawable.commentaryopen);
					}
					commentaryinfos.setMaxLines(3);
					commentaryinfos.setEllipsize(TruncateAt.END);

					flag = true;
				}
			}
		});

		return view;

	}

	public String DateParse(String str1) {
		String[] space = str1.trim().split(" ");
		String[] arrMidLine = space[0].split("-");
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < arrMidLine.length; i++) {
			if (i == 0)
				str.append(arrMidLine[i]).append("年");
			else if (i == 1) {
				str.append(arrMidLine[i]).append("月");
			} else if (i == 2) {
				str.append(arrMidLine[i]).append("日");
			}
		}
		for (int k = 1; k < space.length; k++) {
			str.append(" " + space[k]);
		}
		return str.toString();

	}

}